package com.SuanZhengYi.ZhengZeBiaoDaShi;

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexTest2 {
    public static void main(String[] args) {
        //检验电话，邮箱，时间是否合法。
         checkNumber();
        checkEmail();
        checkTime();
        String data="400-618-9090";
        String regex="(1[3-9]\\d{9})|(0\\d{2,7}-?[1-9]\\d{4,19})|(\\w{2,}@\\w{2,20}(\\.\\w{2,10}){1,2})"
                +"(400-?\\d{3,7}-?\\d{3,7})";
        Pattern pattern=Pattern.compile(regex);
        Matcher matcher= pattern.matcher(data);
        while(matcher.find()){
            String rs= matcher.group();
            System.out.println(rs);
        }
    }

    //检验电话号码
    //19511035073   010-19561992557  01019561992557
    public static void checkNumber() {
        while (true) {
            System.out.println("请你输入你的电话");
            Scanner sc = new Scanner(System.in);
            String num = sc.next();
            if (num.matches("(1[3-9]\\d{9})|(0\\d{2,7}-?[1-9]\\d{4,19})")) {
                System.out.println("你输入的号码格式正确");
            } else {
                System.out.println("你输入的号码格式不正确");
                break;
            }
        }
    }

    //检验邮箱
    public static void checkEmail() {
        while (true) {
            System.out.println("请你输入你的邮箱");
            Scanner sc = new Scanner(System.in);
            String email = sc.next();
            if (email.matches("\\w{2,}@\\w{2,20}(\\.\\w{2,10}){1,2}")) {
                System.out.println("你输入的邮箱正确");
                break;
            } else {
                System.out.println("你输入的邮箱不正确");
            }
        }
    }

    //检验日期
    public static void checkTime() {
        while (true) {
            System.out.println("请你输入你的时间");
            Scanner sc = new Scanner(System.in);
            String time = sc.next();
            //23:14:12
            if(time.matches("\\d[0-23]:\\d[0-59]:\\d[0-59]|\\d[0-23]时\\d[0-59]分\\d[0-59]秒")){
                System.out.println("你输入的时间格式正确");
                break;
            }else {
                System.out.println("你输入的时间格式不正确");
            }
        }
    }
}